﻿using Architecture.Repository.Interface;
using Architeture.Domain;
using Architeture.Domain.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Architecture.Domain.Service
{
    public class GoodService : IGoodService
    {
        protected readonly IGoodsRepsitory goodsRepsitory;

        public GoodService(IGoodsRepsitory goodsRepsitory)
        {
            this.goodsRepsitory = goodsRepsitory;
        }
        /// <summary>
        /// 溯源显示
        /// </summary>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public async Task<GoodsPagedto> GetGoodsInfo(int pageindex, int pagesize, string? gname, string? BPtion, int? ShelfLife, string? PL, string? EPtion)
        {
            var size = (pageindex - 1) * pageindex;
            var list = await goodsRepsitory.GetAllAsync();
            if (gname != null)
            {
                list = list.Where(x => x.GoodsName.Contains(gname)).ToList();
            }
            if (BPtion != null)
            {
                list = list.Where(x => x.BatchofProduction.Contains(BPtion)).ToList();
            }
            if (ShelfLife > 0)
            {
                list = list.Where(x => x.ShelfLife == ShelfLife).ToList();
            }
            if (PL != null)
            {
                list = list.Where(x => x.ProductionLicense.Contains(PL)).ToList();
            }
            if (EPtion != null)
            {
                list = list.Where(x => x.EnterpriseOfProduction.Contains(EPtion)).ToList();
            }
            var page = new GoodsPagedto();
            page.pagecount = list.Count();
            page.totalcount = (int)Math.Ceiling(page.pagecount * 1.0 / pagesize);
            list = list.OrderByDescending(x => x.AddTime).Skip(size).Take(pagesize).ToList();
            page.list = list;
            return page;
        }

        /// <summary>
        /// 溯源添加
        /// </summary>
        /// <param name="a"></param>
        /// <returns></returns>
        public async Task<int> GoodsAdd(GoodsInfo a)
        {
            var lsit = (await goodsRepsitory.GetAllAsync()).Where(x => x.GoodsName == a.GoodsName);
            if (lsit != null)
            {
                a.AddName = "张三";
                a.AddTime = DateTime.Now.ToString();
                await goodsRepsitory.AddAsync(a);
                return a.GoodsInfoid;
            }
            else
            {
                return -1;
            }

        }

        /// <summary>
        /// 溯源删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<int> GoodsInfoDel(List<int> ids)
        {
            var code = 0;
            foreach (var item in ids)
            {
                var list = await goodsRepsitory.GetModelAsync(item);
                if (list != null)
                {
                    goodsRepsitory.Delete(item);
                    code = 1;
                }
                else
                {
                    code = -1;
                }
            }
            return code;
        }
        /// <summary>
        /// 溯源修改
        /// </summary>
        /// <param name="a"></param>
        /// <returns></returns>

        public async Task<int> GoodsUpd(GoodsInfo a)
        {
            a.UpdName = "李四";
            a.UpdTime = DateTime.Now.ToString();
            return await goodsRepsitory.Update(a);
        }

        /// <summary>
        /// 溯源详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<GoodsInfo> SetGoods(int id)
        {
            return await goodsRepsitory.GetModelAsync(id);
        }
    }
}
